package hos.demo

import android.os.Bundle
import android.util.Log
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import com.drake.net.response.convert
import com.drake.net.utils.scopeLife
import hos.demo.ui.theme.NetSoapTheme
import hos.net.soap.Soap
import hos.net.soap.convert.ListMapConvert
import okhttp3.Response

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            NetSoapTheme {
                // A surface container using the 'background' color from the theme
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colorScheme.background
                ) {
                    Column {
                        TextButton(onClick = {
                            scopeLife {
                                val result = Soap<Response>("GetServerDateTime").await()
                                Log.d(
                                    "Soap",
                                    "onCreate: ${result.convert<String>(String::class.java)}"
                                )
                            }
                        }) {
                            Text(text = "Soap 获取时间")
                        }
                        TextButton(onClick = {
                            scopeLife {
                                val result = Soap<Response>("GetLawsStuationPush") {
                                    converter = ListMapConvert()
                                    param("userId", "user_0b5cce19-0be6-46d0-a21a-e0ffab8bdf61")
                                    param("type", "send")
                                    param("keyword", "")
                                    param("_PageNumber", 1)
                                    param("_PageSize", 30)
                                }.await()
                                Log.d(
                                    "Soap",
                                    "ListMap: ${
                                        result.convert<MutableList<MutableMap<String, Any>>>()
                                    }"
                                )
                            }
                        }) {
                            Text(text = "Soap MapList Response")
                        }
                        TextButton(onClick = {
                            scopeLife {
                                val result = Soap<MutableList<MutableMap<String, Any>>>("GetLawsStuationPush") {
                                    converter = ListMapConvert()
                                    param("userId", "user_0b5cce19-0be6-46d0-a21a-e0ffab8bdf61")
                                    param("type", "send")
                                    param("keyword", "")
                                    param("_PageNumber", 1)
                                    param("_PageSize", 30)
                                }.await()
                                Log.d(
                                    "Soap",
                                    "ListMap: $result"
                                )
                            }
                        }) {
                            Text(text = "Soap MapList ")
                        }
                    }
                }
            }
        }
    }
}

@Composable
fun Greeting(name: String, modifier: Modifier = Modifier) {
    Text(
        text = "Hello $name!",
        modifier = modifier
    )
}

@Preview(showBackground = true)
@Composable
fun GreetingPreview() {
    NetSoapTheme {
        Greeting("Android")
    }
}